package com.dormitory.management.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dormitory.management.system.entity.Building;
import com.dormitory.management.system.entity.Housemaster;
import com.dormitory.management.system.entity.Living;
import com.dormitory.management.system.entity.Room;
import com.dormitory.management.system.req.BuildingPageReq;
import com.dormitory.management.system.req.BuildingSaveReq;
import com.dormitory.management.system.req.BuildingUpdateReq;
import com.dormitory.management.system.resp.BuildingResp;
import com.dormitory.management.system.mapper.BuildingMapper;
import com.dormitory.management.system.mapper.HousemasterMapper;
import com.dormitory.management.system.mapper.LivingMapper;
import com.dormitory.management.system.service.BuildingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dormitory.management.system.service.RoomService;
import com.dormitory.managemnet.util.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 楼栋表 服务实现类
 * </p>
 *
 * @author
 * @since 2021-12-17
 */
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {

    @Autowired
    private HousemasterMapper housemasterMapper;
    @Autowired
    private LivingMapper livingMapper;
    @Autowired
    private RoomService roomService;

    @Override
    @Transactional
    public boolean save(BuildingSaveReq req) {
        String str = req.getBuilding();
        String strNew = str.replace("，", ",");
        String[] split = strNew.split(",");
        for (int i = 0; i < split.length; i++) {
            if (!split[i].isEmpty()){
                Building building = BeanUtil.copyProperties(req, Building.class);
                building.setBuilding(split[i]);
                if(this.baseMapper.insert(building)>0){
                    livingMapper.update(null, new UpdateWrapper<Living>().lambda()
                            .setSql("num_building=num_building+1")
                            .eq(Living::getId, req.getLivingId()));
                }
            }
        }
        return true;
    }

    @Override
    @Transactional
    public boolean delete(Long id) {
        Building building = this.baseMapper.selectById(id);
        roomService.deleteAll(id);
        housemasterMapper.update(null,new UpdateWrapper<Housemaster>().lambda()
                .set(Housemaster::getBuildingId,null)
                .eq(Housemaster::getBuildingId,id));
        if(this.baseMapper.deleteById(id)>0){
            int count = roomService.count(new QueryWrapper<Room>().lambda().eq(Room::getBuildingId, id));
            return livingMapper.update(null, new UpdateWrapper<Living>().lambda()
                    .setSql("num_building=num_building-1")
                    .setSql("num_room=num_room-"+count)
                    .eq(Living::getId, building.getLivingId()))>0;
        }
        return false;
    }

    @Override
    public boolean update(BuildingUpdateReq req) {
        return this.baseMapper.updateById(BeanUtil.copyProperties(req,Building.class))>0;
    }

    @Override
    public IPage<BuildingResp> getPage(BuildingPageReq req) {
        return this.baseMapper.getBuildingPage(new Page<>(req.getPageCurrent(),req.getPageSize()), new QueryWrapper<BuildingResp>()
                .like(req.getBuilding()!=null,"building",req.getBuilding())
                .like(req.getHousemaster()!=null,"housemaster",req.getHousemaster())
                .eq(req.getLivingId()!=null,"living_id",req.getLivingId()));
    }
}
